const LOCAL_KEY = 'todomvc'
/**
 * 获取所有任务，从localStorage获取
 */
export function fetch() {
	const res = localStorage.getItem(LOCAL_KEY)
	if (res) {
		return JSON.parse(res)
	}
	return []
}
/**
 * 给我一个数组，我保存到localStorage
 */
export function save(todos) {
	localStorage.setItem(LOCAL_KEY, JSON.stringify(todos))
}

/**
 * 生成一个任务唯一编号，时间戳+4位随机数
 */
export function generateId() {
	return new Date().getTime() + '' + Math.random().toString().substring(2, 5)
}

/**
 * 导出一个filter方法
 */
export function filter(todos, visibility) {
	if (visibility === 'all') {
		return todos
	} else if (visibility === 'active') {
		return todos.filter((it) => !it.completed)
	} else if (visibility === 'completed') {
		return todos.filter((it) => it.completed)
	} else {
		throw new Error('无效筛选值')
	}
}
